<?php if ($this->get('list') == 2){ ?>
<style>
.listings li, .selecting li{ border-bottom: 0px solid #CACACA;cursor:pointer; }
.listings li span, .selecting li span{ width:110px} 
.selected{ -moz-box-shadow: 0px 0px 2px #2ab4c4; -webkit-box-shadow: 0px 0px 2px #2ab4c4;box-shadow: 0px 0px 2px #2ab4c4;padding: 4px;margin:2px; }
.listings li.selected span, .selecting li.selected span{ width:100px} 
#suppliers_item tbody tr:hover{ border-bottom:1px solid #a8dade;border-top:1px solid #a8dade;margin-top:-5px; }
.selected-td{ background-color:#a8dade !important; }
#suppliers_item tbody tr.ordera{ font-weight:bold; }
#show_info, #show_info strong { font-size:13px; }
</style>

<div id="process-item" title="Purchase Order">
</div>

<form class="form_style registed product_page finished" style="width:93%;margin-top:1px;padding-bottom:5px" onsubmit="return false;">
    <div class="entry_content">
	
		<div style="height: 50px;">
      		<select class="selection" id="supplier-list" value="0">
      			<option value="0">Select Supplier</option>
				<?php foreach($sup_suppliers as $sup){ ?>
      			<option value="<?php echo $sup['sdid']; ?>"><?php echo $sup['name']; ?></option>
				<?php } ?>
    		</select>
			<input name="search-sup-item" style="width: 200px;" type="text" placeholder="Search Item" />			
			<span style="display:inline-block;margin:9px;margin-top: 0px;bottom:-5px">
				<input type="checkbox" class="niceCheck" name="showall" ><span style="position: absolute;font-size:11px;display:inline-block;width: 180px;top: -3px;text-indent:5px">Show Pending Ordered Item/s</span>
			</span>
		</div>
		<div style="height: 170px;">
    		<div style="border: 0px solid #e0e0e0;height:220px;width:45%;float:left;overflow: hidden;">
				<table id="suppliers_item">
					<thead>
    					<tr style="display:block;position:relative;">
    						<th class="bg" style="height: 20px;width:150px">Item</th>
    						<th style="height: 20px;width:130px">Quantity</th>
    					</tr>
					</thead>
					<tbody style="height: 190px;overflow: auto;display: block;width:100%;" >
					</tbody>
				</table>
			</div>
    		<div id="show_info" class="ease" style="border: 0px solid #e0e0e0;height: 180px;width: 45%;float: right;padding:20px;box-shadow: 0px 0px 1px #2ab4c4;-moz-box-shadow: 0px 0px 1px #2ab4c4;-webkit-box-shadow: 0px 0px 1px #2ab4c4;overflow:auto;">
			</div>
		</div>
		<div style="border: 1px solid #e0e0e0;overflow: hidden;margin-right: -20px;margin-left: -20px;height: 200px;">
			<table id="for_process">
				<thead>
    				<tr style="display:block;position:relative;">
    					<th style="width:35px;"><input type="checkbox" /></th>
    					<th class="bg" style="width:200px">Supplier</th>
    					<th style="width:200px">Item</th>
    					<th class="bg" style="width:100px">Unit</th>
    					<th style="width:100px">Quantity</th>
    				</tr>
				</thead>
				<tbody style="height: 130px;overflow: auto;display: block;width:100%;">
    				<tr class="list">
    					<td style="width:35px;"></td>
    					<td class="bg" style="width:200px"></td>
    					<td style="width:200px"></td>
    					<td class="bg" style="width:100px"></td>
    					<td style="width:100px"></td>
    				</tr>
				</tbody>
			</table>
		</div>
		<input type="button" id="reorder_process_inlist" style="float: right;margin-top: 5px;" value="Process" />
		<input type="button" id="reorder_remove_inlist" style="float: right;margin-top: 5px;" value="Remove" />
		<div class="clear"></div>
	</div>
</form>
<script>
	var x = 0;
	$('input[name=showall]').parent().click(function(){
		/*if(!$(this).attr('checked')){
			$('#suppliers_item tbody tr.ordera').hide();
		}else{
			$('#suppliers_item tbody tr.ordera').show();
		}*/
		$('input[name=search-sup-item]').trigger('keyup');
	});
	$('#supplier-list').selectBox();
    $('#supplier-list').change(function(){
    	var sup_id = $(this).val();
    	if(!sup_id)
    		return;
    	$.post('?module=pos&action=inventory&action2=reorder&ajax=true',{list:2,getitem:1,id:sup_id},function(data){
    		$('#suppliers_item tbody').html(data).fadeIn();
            $('#suppliers_item tbody tr').click(function(){
            	var sup2_id = $(this).attr('sid');
            	$(this).parent().find('.selected-td').removeClass('selected-td');
            	$(this).children().addClass('selected-td');
				sup_id = $(this).attr('supid');	$.post('?module=pos&action=inventory&action2=reorder&ajax=true',{getinfo:1,id:sup2_id,pid:sup_id},function(data){
                	$('#show_info').html(data).fadeIn();	
					refuncsup(sup2_id);
                });
            }).each(function(){
				if($(this).hasClass('ordera')){
					if(!$('input[name=showall]').attr('checked'))
						$(this).hide();
				}
			});	
			<?php if (isset($single) && COUNT($single)>1){ ?>	
    		if(!x){	
            	$('#suppliers_item tbody tr').each(function(){
            		if($(this).attr('sid') == '<?php echo $single[0]; ?>')
            			$(this).trigger('click');
            	});
				x++;
			}
			<?php } ?>
    	});
    });
	$('#supplier-list').trigger('change');
	$('input[name=search-sup-item]').keyup(function(){
		var key = $(this).val().toLowerCase();
		var isc = $('input[name=showall]').is(':checked');
		$('#suppliers_item tbody tr').each(function(){
    		if(key.length > 0){
    			$(this).hide();
    			if($(this).find('.bg').text().toLowerCase().indexOf(key) > -1){
    					$(this).fadeIn();
    			}
			}else{
					$(this).fadeIn();
			}
			if(!isc)
				$('#suppliers_item tbody tr.ordera').hide();
		});
	});
<?php if (isset($single) && COUNT($single)>1){ ?>	
$('#suppliers_item tbody').ready(function(){
	$('#supplier-list').val(<?php echo $single[1]; ?>).trigger('change');
});
<?php } ?>

	$("input[name=showall].niceCheck").each(function() {
		changeCheckStart($(this));
    });
var refuncsup = function(sup_id){
	$('#add_reorder').click(function(e){
		e.preventDefault();
		if($('#'+$('#reorder-temp').attr('var')).length){
			if(confirm("Already Exist! Merge with previous?")){
				var tt = $('#'+$('#reorder-temp').attr('var')).find('td:last-child');
				var prev = tt.text()*1;
				tt.text(prev + $('#add_reorder_qty').val()*1);
			}
			return;
		}
		var vals = $('#reorder-temp').html().split(",");
		var vl = '<tr class="list" pid="' + $('#reorder-temp').attr('pid') + '" iid="' + $('#reorder-temp').attr('iid') + '" id="' + $('#reorder-temp').attr('var') + '">' +
						'<td><input type="checkbox" /></td>' +
    					'<td class="bg">' + vals[0] + '</td>' +
    					'<td>' + vals[1] + '</td>' +
    					'<td class="bg">' + vals[2] + '</td>' +
    					'<td>' + $('#add_reorder_qty').val() + '</td>' +
    			'</tr>';
		$('#for_process tbody').prepend(vl);
	});
	$('.supps').click(function(e){
		e.preventDefault();
		$.post('?module=pos&action=inventory&action2=reorder&ajax=true',{getinfo:1,id:sup_id,pid:$(this).attr('sup')},function(data){
            $('#show_info').html(data).fadeIn();
			refuncsup(sup_id);
		});
	});
};
$('#process-item').dialog({
	height: 600,
	width: 800,
    modal: true,
	show: {
        effect: "drop",
        duration: 700
    },
	autoOpen: false
});
$('#reorder_process_inlist').click(function(){
	var last = $('#for_process tbody tr:first-child').attr('pid');
	if (typeof last === "undefined") 
		return;
	var reorders=new Array(); 
	var x = 0;
	$('#for_process tbody tr').each(function(){
		var pid = $(this).attr('pid');
		var iid = $(this).attr('iid');
		var qty = $(this).find('td:last-child').text();
		if( pid == last ){
			reorders[x++] = new Array(pid, iid, qty);
			$(this).attr('inprocess', 1);
		}
	});
	process_reorders(reorders);
/*
    $.post('?module=pos&action=inventory&action2=reorder&ajax=true&list=3', function(data) {
        $('#process-item').html(data).parent().css('overflow','hidden');
    	$('#process-item').dialog('open');
    });
*/
});
var process_reorders = function(reorders){
	$.ajax({
       type: "POST",
       data: {orders:reorders,isprocessorder:1},
       url: "?module=pos&action=inventory&action2=reorder&ajax=true",
       success: function(data){
	   		console.log(data);
    		$('#process-item').html(data).parent().css('overflow','hidden');
			$('#process-item').dialog({
            	beforeClose: function(event, ui) { 
            		//$('#reorder_process_inlist').trigger('click');
            	}
			});
    		$('#process-item').dialog('open');
       }
	});
}
$('#for_process thead th:first-child input').click(function(){
	$('#for_process tbody').find('input').prop('checked', $(this).prop('checked'));
});
$('#reorder_remove_inlist').click(function(){
	$('#for_process tbody').find('input:checked').parent().parent().fadeOut('slow', function() { $(this).remove(); });
});
</script>
<?php } else if($this->get('isprocessorder')) { ?>
<script>
var fsending = function(){
	var po = $('input[name=po]').val();
	var sdid = $('input[name=sdid]').val();
	var t = 0;
	var reorders2 = new Array();
	$('#for-orders tr').each(function(){
		var iid = $(this).attr('iid');
		var qty = $(this).attr('qty');
		var imdid = $(this).attr('imdid');
		var cost = $(this).attr('cost');
		reorders2[t++] = new Array(iid, imdid, qty, cost);
	});
	$.ajax({
       type: "POST",
       data: {processing:1,po:po,sdid:sdid,reorders:reorders2},
       url: "?module=pos&action=inventory&action2=reorder&ajax=true",
       success: function(data){
	   		console.log(data);
			set_notif('success', "New Item Ordered. <a class='toast-close reorder-list' href='#'>See</a> list.", "if(typeof refunc_items == 'function'){refunc_items();}", 0);
            $('#process-item').dialog('close');
        	$('#for_process tbody tr').each(function(){
        		if($(this).attr('inprocess')){
            		$(this).fadeOut('slow', function() {
            			$(this).remove();
						$('#supplier-list').trigger('change');
        				if($('#for_process tbody tr[inprocess=1]').length == 0){
        					$('#reorder_process_inlist').trigger('click');
        				}
            		});
        		}
        	});
       }
	});
    return false;
};
$('.bcan').click(function(){
	$('#process-item').dialog('close');
});
</script>
<?php if ($isconfirming) {?>
<link href="<?php $this->path('css/custom.css');?>" rel="stylesheet" media="all" />
<link href="<?php $this->path('css/base.css');?>" rel="stylesheet" media="all" />
<link href="<?php $this->path('css/jquery-ui-1.10.3.custom.min.css');?>" rel="stylesheet" media="all" />
<link href="<?php $this->path('css/breeze.css');?>" rel="stylesheet" media="all" />
<link rel="shortcut icon" type="image/gif" href="<?php $this->path('img/favicon.ico');?>">
<script type="text/javascript" src="<?php $this->path('js/jquery-1.8.2.min.js');?>"></script>
<script type="text/javascript" src="<?php $this->path('js/jquery-1.8.js');?>"></script>
<script type="text/javascript" src="<?php $this->path('js/jquery-ui.js');?>"></script>
<script type="text/javascript" src="<?php $this->path('js/ui/ui.dialog.js');?>"></script>
<div style="width:900px;margin-top:50px;margin-left:auto;margin-right:auto;">
<style>
@media print
{    
    .no-print, .no-print *
    {
        display: none !important;
    }
}
</style>
<?php }else{ ?>
<div style="height: 530px;overflow:auto;">	
<?php } ?>
	<?php if (!$ispreview && !$isconfirming) { ?>
	<strong>TO: </strong><span>&emsp;<?php echo $supplierr['email']; ?></span><br />
	<strong>FROM: </strong><span>&emsp;lvtmerchandise@yahoo.com</span><br />
	<hr style="margin:10px 0 10px 0" />
	<?php } ?>
	<form id="send-po" onsubmit="return fsending();" class="form_style registed product_page finished" style="width:93%;margin-top:1px;">
    	<div style="height: auto;overflow: auto;width: 100%;">
			<?php if (!$ispreview && !$isconfirming) { ?>
			<span style="position:relative;display:inline-block;float:right;"><?php echo $po['datenow']; ?></span>
        	<h4>Purchase Order</h4>
			<strong>PO #:</strong>&emsp;<span><?php echo $po_num; ?></span><br /><br />
        	<div style="float:left;width:50%;">
        		<strong>VENDOR:</strong><br />
    			<span><?php echo $supplierr['name']; ?>,</span><br />
    			<span><?php echo $supplierr['address']; ?></span><br />
    			<span><?php echo $supplierr['contact']; ?></span><br /><br />
				<strong>Contact:</strong>
    			<span><?php echo $supplierr['representative']; ?></span>
        	</div>
        	<div style="float:right;width:50%;">				
        		<strong>SHIP TO:</strong><br />
    			<span>LVT General Merchandise,</span><br />
    			<span>Balaoan, La Union</span><br />
    			<span>+639189650271</span>
        	</div>
			<br />
			<?php } else if ($isconfirming) { ?>
			<span style="position:relative;display:inline-block;float:right;"><?php echo $po['datenow']; ?></span>
        	<h4>Purchase Order</h4>
			<strong>PO #:</strong>&emsp;<span><?php echo $rotid ?></span><br /><br />
        	<div style="float:left;width:50%;">
        		<strong>VENDOR:</strong><br />
    			<span><?php echo $supplierr['name']; ?>,</span><br />
    			<span><?php echo $supplierr['address']; ?></span><br />
    			<span><?php echo $supplierr['contact']; ?></span><br /><br />
				<strong>Contact:</strong>
    			<span><?php echo $supplierr['representative']; ?></span>
        	</div>
        	<div style="float:right;width:50%;">				
        		<strong>SHIP TO:</strong><br />
    			<span>LVT General Merchandise,</span><br />
    			<span>Balaoan, La Union</span><br />
    			<span>+639189650271</span>
        	</div>
			<br />
			<?php } else { ?>
			<h4>Purchase Order To <?php echo $orders[0]['name']?> [<?php echo $rotid ?>]</h4>
			<?php } ?>
        	<table style="float:left;">
				<thead>
            		<tr>
            			<th class="bg" style="width:30%">Item Name</th>
            			<th style="width:20%">Unit</th>
            			<th class="bg" style="width:10%">Quantity</th>
            			<th style="width:20%">Unit Price</th>
            			<th class="bg" style="width:20%">Total</th>
            		</tr>
				</thead>
				<tbody id="for-orders">
					<?php
						$total = 0;
						if (!$ispreview && !$isconfirming )
    					{
    					 	foreach($orders as $order){
    							$total = $total + getDetail($order[1], $order[0], $order[2]);
    						}
						}else{
							foreach($orders as $order){
    							$total = $total + getDetail($order['iid'], $order['sdid'], $order['quantity']);
							}
						}
					?>
        			<tr>
        				<td colspan="5" style="height: 35px;vertical-align: middle;border-collapse: collapse;text-align: right;"><strong style="font-size:14px;">Total:</strong>&emsp;<span class="ui-text-center" style="border-bottom:1px solid #000;width:20%;display:inline-block;">&#8369; <?php echo number_format($total, 2, '.', ','); ?></span></td>
        			</tr>
				</tbody>
        	</table>
    	</div>
		<?php if (!$ispreview && !$isconfirming) { ?>
    	<input type="submit" style="float: right;margin-top: 15px;" value="Send" />
    	<input type="button" class="bcan" style="float: right;margin-top: 15px;" value="Cancel" />
		<input name="sdid" type="hidden" value="<?php echo $supplierr['sdid']; ?>" />
		<input name="po" type="hidden" value="<?php echo $po_num; ?>" />
		<?php } else if ($isconfirming) {?>
		<?php if ($orders[0]['status'] == 0){ ?>
    	<input type="button" class="no-print" style="float: right;margin-top: 15px;" value="Cancel" />
    	<input type="button" class="no-print" style="float: right;margin-top: 15px;" value="Confirm" />
		<?php } ?>
    	<input type="button" class="no-print" style="float: right;margin-top: 15px;" value="Print" />
		<?php }else{ ?>
			<input type="button" onclick="javascript:$('#displayer').dialog('close');" style="float: right;margin-top: 15px;" value="Close" />
			<?php if($orders[0]['status'] != 10){ ?>
			<input type="button" onclick="javascript:$('#displayer').dialog('close');" style="float: right;margin-top: 15px;" value="Re-Send" />
			<?php } ?>
		<?php } ?>
    	<div class="clear"></div>
	</form>
</div>
    <?php if ($isconfirming) { ?>
		<div id="confirming" title="Confirm Action">
			<form onsubmit="return confirm();" class="form_style " style="margin-top:1px;">
				<h4>Remarks</h4>
				<textarea id="sup_remarks" style="width: 99%;height: 200px;"></textarea>
				<br />
				<hr />
				<br />
            	<input id="done_sup" ok="1" type="button" value="Done" />
				<div class="clear"></div>
			</form>
		</div>
		<script>
			$('#done_sup').click(function(){
				var stat = $(this).attr('ok');
				var mes = $('#sup_remarks').val();
				$.post('?module=pos&action=inventory&action2=reorder&ajax=true',{confirms:stat,message:mes,rotid:<?php echo $rotid ?>},function(data){
					if(data == 1){
						$('input[value=Cancel]').hide();
						$('input[value=Confirm]').hide();
						$('#confirming').dialog('close');
					}else{
						console.log(data);
					}
				});
			});
  			$('#confirming').dialog({
  				autoOpen: false,
        		height: 380,
        		width: 400,
        		modal: true,
  				resizable: false,
              	hide: {
                	effect: "fade",
                	duration: 400
                }
  			});
  			$('input[value=Print]').click(function(){
				window.print();
			});
  			$('input[value=Cancel]').click(function(){ 
				$('#sup_remarks').val("");
				$('#confirming').find('form h4').text("Reason for Canceling");
				$('#confirming').dialog('open');
				$('#done_sup').attr('ok',2);
			});
  			$('input[value=Confirm]').click(function(){
				$('#sup_remarks').val("");
				$('#confirming').find('form h4').text("Remarks");
				$('#confirming').dialog('open');
				$('#done_sup').attr('ok',1);
			});
		</script>
    <?php } ?>
<?php } ?>